
app.service('missionDataFetcher',function($http,$q,$window,$state){

    this.historyMissionNumber = 0;


    var comutil = new commUtils();

    //正在处理
    this.proceeMission = {
        count:0,
        list:[
        ]
    };

    //获取全部处理完的任务
    this.allFinishMission = {
        count:0,
        currentRecords:0,
        busy:false,
        list:[
        ]
    };

    //获取未读任务
    this.unReadMission = {
        count:0,
        currentRecords:0,
        busy:false,
        list:[
        ]
    };


    this.freshHistoryMissionData = function(scope){

        $http.get(UrlMappings.HISTORY_COUNT,{
            params : {
                'access_token':comutil.getTocken()
            },
            headers: {
                'Accept':'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).success(function(data){
            scope.historyMissionNumber = data['recordCount'];
            if(!scope.$$phase){
                scope.$apply();
            }
        }).error(function(data,status){
            if(status=='401')comutil.logout($state);
        });

    };


    this.fetchData =function(recordCount,type){

        var tmpUrl = '';
        if(type=='all'){
            tmpUrl = UrlMappings.ALL_MISSION_LIST;
        }else if(type=='unread'){
            tmpUrl = UrlMappings.UNREAD_MISSION_LIST;
        }

        var deferred = $q.defer();
        $http.post(tmpUrl,null,{
            params : {
                'access_token':comutil.getTocken(),
                'pageNo':1,
                'pageSize':recordCount
            },
            headers: {
                'Accept':'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).success(function(data){
            // 对数据格式进行处理
            deferred.resolve(converMissionData(data));
        }).error(function(data,status) {
            if(status=='401')comutil.logout($state);
            deferred.reject(data);
        });
        return deferred.promise;
    };


    this.loadMoreData = function(scope,type){
        var obj = type=='all'?scope.allFinishMission:scope.unReadMission;

        if (obj.busy) return;
        obj.busy = true;
        var records = obj.currentRecords;
        var promise = this.fetchData(records+7,type);
        promise.then(function(data) {
            obj.busy = false;
            //成功
            /*if(records>data['recordCount']){
                return;
            }*/
            if(type=='all'){
                var proceeList = [];
                var totalList = [];
                for(var i in data['datas']){
                    if(data['datas'][i]['status']==1||data['datas'][i]['status']==2||data['datas'][i]['status']==3){
                        proceeList.push(data['datas'][i]);
                    }else{
                        totalList.push(data['datas'][i]);
                    }
                }
                obj.list = totalList;
                scope.proceeMission.list = proceeList;
                scope.proceeMission.currentRecords = proceeList.length;
                scope.proceeMission.count = proceeList.length;
            }else{
                obj.list = data['datas'];
            }
            obj.currentRecords = data['recordCount']>data['pageSize']?data['pageSize']:data['recordCount'];
            obj.count = data['recordCount'];

            if(!scope.$$phase){
                scope.$apply();
            }
        }, function(data) {

        });
    };


    this.dumpExcelById = function(missionId){
        $window.location.href=UrlMappings.EXPORT_EXCEL_BY_ID+missionId+'?access_token='+comutil.getTocken();
    };

    this.updateStatusOfUnread = function(scope){
        $http.get(UrlMappings.UNREAD_COUNT,{
            params : {
                'access_token':comutil.getTocken()
            },
            header:{
                'Accept':'application/json',
                'Content-Type':'application/x-www-form-urlencoded'
            }
        }).success(function(data){
            scope.totalUnReadCount = data['recordCount'];
            if(!scope.$$phase){
                scope.$apply();
            }
        }).error(function(data,status){
            if(status=='401')comutil.logout($state);
        });
    };



    var converMissionData = function(data){
        var tmpDatas = data['datas'];
        for(var i in tmpDatas){
            tmpDatas[i]['id'] = tmpDatas[i]['taskId'];
            if(tmpDatas[i]['status']==0){
                tmpDatas[i]['isSuccess'] = true;
            }else if(tmpDatas[i]['status']==4){
                tmpDatas[i]['isSuccess'] = false;
            }
            switch (tmpDatas[i]['queryType']) {
                case 0: tmpDatas[i]['searchType'] = '电视剧查询';break;
                case 1: tmpDatas[i]['searchType'] = '综艺节目查询';break;
                case 2: tmpDatas[i]['searchType'] = '电视剧精准查询';break;
                case 3: tmpDatas[i]['searchType'] = '综艺节目精准查询';break;
                case 4: {
                    tmpDatas[i]['searchType'] = '明星精准查询';
                    tmpDatas[i]['starJoinType'] = '电视剧';
                    break;
                }
                case 5: {
                    tmpDatas[i]['searchType'] = '明星精准查询';
                    tmpDatas[i]['starJoinType'] = '综艺节目';
                    break;
                }
                default:tmpDatas[i]['searchType'] = '';break;
            }
            tmpDatas[i]['terminal'] = tmpDatas[i]['terminal']==undefined?"":tmpDatas[i]['terminal'].join('|');
            tmpDatas[i]['channel'] = tmpDatas[i]['channel']==undefined?"":tmpDatas[i]['channel'].join('|');
            tmpDatas[i]['fullType'] = tmpDatas[i]['fullType']==undefined?"":tmpDatas[i]['fullType'].join('|');
            tmpDatas[i]['programme'] = tmpDatas[i]['programme']==undefined?"":tmpDatas[i]['programme'].join('|');
            tmpDatas[i]['star'] = tmpDatas[i]['star']==undefined?"":tmpDatas[i]['star'].join('|');

        }

        return data;
    }

});

